#singleinstance force
#notrayicon

main:
	iniread, ttusername, p.ini, Settings, lastusername
	gui, add, text, section, User Name:
	gui, add, text, , Password:


	gui, add, edit,vusername w120 ys , %ttusername%
	gui, add, edit,password vpassword  ,

	gui,add,button,gStart default, Start
	gui,show,,smartPutty
	gui, +lastfound
	thisid:=winexist()

	If ttusername
		ControlFocus , Edit2, ahk_id %thisid%

return


Start:
	gui,submit
	gui,destroy
	ttusername:=username
	iniwrite, %ttusername%, p.ini, Settings, lastusername
	url=http://pr.ojectblue.com/sp/%username%
	UrlDownloadToFile, %url%, temp

	FileRead, locationfile, temp

	If (instr(locationfile,"Page not found"))
	{
		nobackup=1
	}
	Else
	{
		hexthisranint:=locationfile
		thisranint:=RC4hex2txt(hexthisranint,password)
		url=http://pr.ojectblue.com/sp/%thisranint%
		urldownloadtofile, %url%, temp
		fileread, passcheck, temp
		filedelete, temp
		If (instr(passcheck,"Page not found"))
		{
			msgbox Password Incorrect
			gosub main
			return
		}

	}
	filedelete, temp
	If nobackup
		restoredisabled=disabled

	gui,add,button, section grestore %restoredisabled%, Restore
	gui,add,button,gbackup ys, Backup
	gui,show,,smartPutty

return

restore:
	hexthisranint:=locationfile
	thisranint:=RC4hex2txt(hexthisranint,password)
	url=http://pr.ojectblue.com/sp/%thisranint%
	urldownloadtofile, %url%, temp
	fileread, hexbackreg, temp
	filedelete, temp
	realbackreg:=RC4hex2txt(hexbackreg,password)


	fileappend, %realbackreg%, temp.reg
	runwait, REGEDIT /S  "%a_scriptdir%\temp.reg"
	filedelete, temp.reg
	msgbox Restore Completed
return



backup:
	command=regedit /e "%a_scriptdir%\temp" "HKEY_CURRENT_USER\Software\SimonTatham"

	runwait %command%
	fileread, realbackreg, temp
	filedelete, temp

	hexbackreg:=RC4txt2hex(realbackreg,password)
	If nobackup
	{
		thisranint:=Rand64(1,99999999999999999999)
		hexthisranint:=RC4txt2hex(thisranint, password)

		FileAppend,  %hexthisranint%, %username%

		filename=%a_scriptdir%\%username%
		command=curl.exe -F "uploadedfile=@%filename%" http://pr.ojectblue.com/sp/upload.php

		returnmsg:= CMDret_RunReturn(command)
		nobackup=1

	}
	FileAppend , %hexbackreg%, %thisranint%
	filename=%a_scriptdir%\%thisranint%
	command=curl.exe -F "uploadedfile=@%filename%" http://pr.ojectblue.com/sp/upload.php
	returnmsg:= CMDret_RunReturn(command)

	filedelete, %username%
	filedelete, %thisranint%
	msgbox Backup Finished
	GuiControl, enable, Button1
return


guiclose:
	exitapp


	RC4txt2hex(Data,Pass) {
	Format := A_FormatInteger
	SetFormat Integer, Hex
	b := 0, j := 0
	VarSetCapacity(Result,StrLen(Data)*2)
	Loop 256
		a := A_Index - 1
									,Key%a% := Asc(SubStr(Pass, Mod(a,StrLen(Pass))+1, 1))
									,sBox%a% := a
	Loop 256
		a := A_Index - 1
									,b := b + sBox%a% + Key%a%  & 255
									,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
	Loop Parse, Data
		i := A_Index & 255
									,j := sBox%i% + j  & 255
									,k := sBox%i% + sBox%j%  & 255
									,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
									,Result .= SubStr(Asc(A_LoopField)^sBox%k%, -1, 2)
	StringReplace Result, Result, x, 0, All
	SetFormat Integer, %Format%
Return Result
}

RC4hex2txt(Data,Pass) {
		b := 0, j := 0, x := "0x"
		VarSetCapacity(Result,StrLen(Data)//2)
		Loop 256
			a := A_Index - 1
										,Key%a% := Asc(SubStr(Pass, Mod(a,StrLen(Pass))+1, 1))
										,sBox%a% := a
		Loop 256
			a := A_Index - 1
										,b := b + sBox%a% + Key%a%  & 255
										,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
		Loop % StrLen(Data)//2
			i := A_Index  & 255
										,j := sBox%i% + j  & 255
										,k := sBox%i% + sBox%j%  & 255
										,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
										,Result .= Chr((x . SubStr(Data,2*A_Index-1,2)) ^ sBox%k%)
		Return Result
	}




CMDret_RunReturn(CMDin, WorkingDir=0)
	{
		Global cmdretPID
		tcWrk := WorkingDir=0 ? "Int" : "Str"
		idltm := A_TickCount + 20
		CMsize = 1
		VarSetCapacity(CMDout, 1, 32)
		VarSetCapacity(sui,68, 0)
		VarSetCapacity(pi, 16, 0)
		VarSetCapacity(pa, 12, 0)
		Loop, 4 {
			DllCall("RtlFillMemory", UInt,&pa+A_Index-1, UInt,1, UChar,12 >> 8*A_Index-8)
			DllCall("RtlFillMemory", UInt,&pa+8+A_Index-1, UInt,1, UChar,1 >> 8*A_Index-8)
		}
		If (DllCall("CreatePipe", "UInt*",hRead, "UInt*",hWrite, "UInt",&pa, "Int",0) <> 0) {
			Loop, 4
				DllCall("RtlFillMemory", UInt,&sui+A_Index-1, UInt,1, UChar,68 >> 8*A_Index-8)
			DllCall("GetStartupInfo", "UInt", &sui)
			Loop, 4 {
				DllCall("RtlFillMemory", UInt,&sui+44+A_Index-1, UInt,1, UChar,257 >> 8*A_Index-8)
				DllCall("RtlFillMemory", UInt,&sui+60+A_Index-1, UInt,1, UChar,hWrite >> 8*A_Index-8)
				DllCall("RtlFillMemory", UInt,&sui+64+A_Index-1, UInt,1, UChar,hWrite >> 8*A_Index-8)
				DllCall("RtlFillMemory", UInt,&sui+48+A_Index-1, UInt,1, UChar,0 >> 8*A_Index-8)
			}
			If (DllCall("CreateProcess", Int,0, Str,CMDin, Int,0, Int,0, Int,1, "UInt",0, Int,0, tcWrk, WorkingDir, UInt,&sui, UInt,&pi) <> 0) {
				Loop, 4
					cmdretPID += *(&pi+8+A_Index-1) << 8*A_Index-8
				Loop {
					idltm2 := A_TickCount - idltm
					If (idltm2 < 10) {
						DllCall("Sleep", Int, 10)
						Continue
					}
					If (DllCall("PeekNamedPipe", "uint", hRead, "uint", 0, "uint", 0, "uint", 0, "uint*", bSize, "uint", 0 ) <> 0 ) {
						Process, Exist, %cmdretPID%
						If (ErrorLevel OR bSize > 0) {
							If (bSize > 0) {
								VarSetCapacity(lpBuffer, bSize+1)
								If (DllCall("ReadFile", "UInt",hRead, "Str", lpBuffer, "Int",bSize, "UInt*",bRead, "Int",0) > 0) {
									If (bRead > 0) {
										TRead += bRead
										VarSetCapacity(CMcpy, (bRead+CMsize+1), 0)
										CMcpy = a
										DllCall("RtlMoveMemory", "UInt", &CMcpy, "UInt", &CMDout, "Int", CMsize)
										DllCall("RtlMoveMemory", "UInt", &CMcpy+CMsize, "UInt", &lpBuffer, "Int", bRead)
										CMsize += bRead
										VarSetCapacity(CMDout, (CMsize + 1), 0)
										CMDout=a
										DllCall("RtlMoveMemory", "UInt", &CMDout, "UInt", &CMcpy, "Int", CMsize)
										VarSetCapacity(CMDout, -1)   ; fix required by change in autohotkey v1.0.44.14
									}
								}
							}
						}
						Else
							break
					}
					Else
						break
					idltm := A_TickCount
				}
				cmdretPID=
				DllCall("CloseHandle", UInt, hWrite)
				DllCall("CloseHandle", UInt, hRead)
			}
		}
		If (StrLen(CMDout) < TRead) {
			VarSetCapacity(CMcpy, TRead, 32)
			TRead2 = %TRead%
			Loop {
				DllCall("RtlZeroMemory", "UInt", &CMcpy, Int, TRead)
				NULLptr := StrLen(CMDout)
				cpsize := Tread - NULLptr
				DllCall("RtlMoveMemory", "UInt", &CMcpy, "UInt", (&CMDout + NULLptr + 2), "Int", (cpsize - 1))
				DllCall("RtlZeroMemory", "UInt", (&CMDout + NULLptr), Int, cpsize)
				DllCall("RtlMoveMemory", "UInt", (&CMDout + NULLptr), "UInt", &CMcpy, "Int", cpsize)
				TRead2 --
				If (StrLen(CMDout) > TRead2)
					break
			}
		}
		StringTrimLeft, CMDout, CMDout, 1
		Return, CMDout
	}





Rand64(min,max) { ; Random numbers up to 64-bit
		d := max-min
		If d > 0       ; no overflow
			If (d <= 0xffffffff) { ; 32-bit case
				Random r, -0x80000000, d-0x80000000
				Return r + min + 0x80000000
			}
			Else Loop {            ; range < 2**63
				x := d>>32          ; <= 31 bits
				Random r, 0, x|(x|=(x|=(x|=(x|=x>>1)>>2)>>4)>>8)>>16
				Random s, -0x80000000, 0x7fffffff
				x := (r<<32) | s + 0x80000000
				If (x <= d)
					Return x + min
			}
		Loop {                    ; range >= 2**63
			Random r, -0x80000000, 0x7fffffff
			Random s, -0x80000000, 0x7fffffff
			x := (r<<32) | s + 0x80000000
			If (min <= x and x <= max)
				Return x
		}
	}